<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.6.8/css/layui.min.css" />
    <script src="https://cdn.staticfile.org/layui/2.6.8/layui.min.js" ></script>
    <script src="../../res/js/mz.form.js"></script>
    <script src="../../res/js/mz.common.js"></script>
</head>
<body>

<div style=" margin: 10px auto;">

    <div style="margin: 30px;">
        环境选择：
        <select id="opts" class="layui-select">
            <option value="0">市民之家/武汉分散</option>
            <option value="1">东湖高新</option>
            <option value="2">十堰</option>
            <option value="3">测试</option>
        </select>

        <a href="http://47.98.218.21:8080/xxl-job-admin/" target="_blank">xxl-job</a>
    </div>

    <form id="data_form" class="layui-form layui-form-pane" lay-filter="data_form">
        <input type="hidden" name="a" value="com.mysql.jdbc.Driver"  class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label">链接</label>
            <div class="layui-input-block">
                <input type="text" name="b"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="c"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="d"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="*">提交</button>
                <button id="reset" onclick="return false;" class="layui-btn">重置为默认</button>
            </div>
        </div>
    </form>
</div>


<script>

    var dbs = [
        {b: 'jdbc:mysql://10.100.10.28:3306/zfcg_dev?useUnicode=true&characterEncoding=UTF-8', c: 'ztb', d: '654321'},
        {b: 'jdbc:mysql://10.100.10.28:3306/zfcg_dhgx_dev?useUnicode=true&characterEncoding=UTF-8', c: 'ztb', d: '654321'},
        {b: 'jdbc:mysql://10.100.10.28:3306/zfcg_sy?useUnicode=true&characterEncoding=UTF-8', c: 'ztb', d: '654321'},
        {b: 'jdbc:mysql://172.16.97.67:3306/notice_test?useUnicode=true&characterEncoding=utf-8&autoReconnect=false&useSSL=false&serverTimezone=GMT%2B8', c: 'ztb', d: '654321'},
    ]

    layui.$("#opts").change(function () {
        layui.form.val('data_form', dbs[layui.$(this).val()]);
    });

    layui.$("#reset").click(function () {
        layui.$.post(base_context + '/resetDefault', function () {
            layer.msg('重置成功！');
        })
    });

    layui.use(['form'], function(){
        layui.form.render();

        layui.form.val('data_form', dbs[0]);

        layui.form.on('submit(*)', function(data){
            layui.$.ajax({
                url: base_context + '/resetDatabase',
                type: 'POST',
                contentType: "application/json",
                data: JSON.stringify(data.field),
                dataType: "json",
                processData: false,
                async: false,
                success: function () {
                    layui.$.ajax({
                        url: base_url + '/sql_test',
                        type: 'GET',
                        timeout: 3000,
                        success: function (data) {
                            if(data.content && data.content.length){
                                layer.msg('切换成功！');
                            }else{
                                layer.msg('切换失败，请重新设置！');
                            }
                        },
                        error: function () {
                            layer.msg('切换失败，请重新设置！');
                        }
                    });
                }
            })
            return false;
        });
    });



</script>

</body>
</html>
